<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/******************************************************
* Profile			: template 
* Author			: 乔伟
* Create Time		: 2011-10-05 
* Modify Time		: 2011-10-05 
* Modify Profile	:  
******************************************************/
class template extends Admin_Controller 
{
	/******************************************************
	 * Profile			: 列表
	* Author			: 乔伟
	* Create Time		: 2011-10-05
	* Modify Time		: 2011-10-05
	* Modify Profile	:
	******************************************************/
	public function index()
	{
		$where = $this->Request();
	
		!isset($where['page_size']) && $where['page_size'] = 20;
		$where['order_by'] = "id desc";
		$templates = $this->template_manager->get_list($where);
	
		$page_size = 20; $page = isset($_GET['page'])?$_GET['page']:1;
		// p_r($templates);exit;
		$total_count = $this->template_manager->query_result->record_count;
		$page_count = intval($total_count/$page_size) + 1;
		$pre_page = ($page - 1) < 1 ? 1 : ($page - 1);
		$next_page = ($page + 1) > $page_count ? $page_count : ($page + 1);
	
		$get_str = "";
		if(!isset($_GET))
		{
			$get_str = "?";
		}
		else
		{
			$tmp = array();
			foreach($_GET as $key=>$value)
			{
				if($key != "template")
				{
					$tmp[] = "$key=$value";
				}
			}
			$get_str = "?".implode('&',$tmp)."&";
		}
	
		$sp_config = array(
				'base_url' => _U('template/index'.$get_str,true),
				'current_page'=>$page,
				'pre_page'=>$pre_page,
				'next_page'=>$next_page,
				'page_count'=>$page_count
		);
	
		$params = array(
				'templates' => $templates ,
				'sp_config' => $sp_config
		);
	
		$this->load->view("admin/template/v_index",$params);
	}
	
	/******************************************************
	* Profile		: 编辑
	* Author		: 乔伟
	* Create Time		: 2011-10-05 
	* Modify Time		: 2011-10-05
	* Modify Profile	:  
	******************************************************/
	public function apply($name = null)
	{
		$name = isset($_GET['name'])?$_GET['name']:$name;
		if(!empty($name))
		{
			$this->site_info_manager->update(array('global_template'=>$name));
		}
		
		$this->push_error('iframe_target_url',$this->last_index_page());   
		
		$this->load->view("admin/template/v_apply");  
		// echo $this->pop_error('iframe_target_url');
		// echo $this->pop_error('iframe_target_url');exit; 
		// echo "<script>parent.location.reload();</script>" ; 
		// $this->goto_last_index_page() ; 
	}
	
	/******************************************************
	* Profile		: 删除
	* Author		: 乔伟
	* Create Time		: 2011-10-05 
	* Modify Time		: 2011-10-05
	* Modify Profile	:  
	******************************************************/
	public function del($id = null) 
	{
		$this->template_manager->delete($id);
		// $this->goto_last_index_page() ;
	}
}
